using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace View.Common
{
	public class IniFile
	{
		private bool largeCommentFlag = false;
		private Dictionary<string, Dictionary<string, string>> iniCahce = new Dictionary<string, Dictionary<string, string>>();
		public string FileName
		{
			get;
			set;
		}
		public IniFile(string INIFile)
		{
			this.FileName = INIFile;
		}
		public bool Load()
		{
			bool result;
			if (!File.Exists(this.FileName))
			{
				FileInfo fileInfo = new FileInfo(this.FileName);
				if (!Directory.Exists(fileInfo.Directory.FullName))
				{
					Directory.CreateDirectory(fileInfo.Directory.FullName);
				}
				FileStream fileStream = File.Create(this.FileName);
				fileStream.Close();
				result = false;
			}
			else
			{
				StreamReader streamReader = new StreamReader(File.Open(this.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.Default);
				if (streamReader == null)
				{
					result = false;
				}
				else
				{
					bool flag = false;
					Dictionary<string, string> dictionary = null;
					string secString;
					while (true)
					{
						string text = this.ReadLine(streamReader);
						if (text == null)
						{
							break;
						}
						text = text.Trim();
						if (!(text == ""))
						{
							if (text.StartsWith("/*"))
							{
								this.largeCommentFlag = true;
							}
							else
							{
								if (this.largeCommentFlag)
								{
									if (text.StartsWith("*/"))
									{
										this.largeCommentFlag = false;
									}
								}
								else
								{
									if (text.Length >= 2)
									{
										if (text[0] == ';' && text[1] == ';')
										{
											continue;
										}
									}
									secString = IniFile.GetSecString(text);
									if (secString != "")
									{
										if (secString.Length >= 2)
										{
											if (secString[0] == ';' && secString[1] == ';')
											{
												flag = true;
												continue;
											}
											flag = false;
										}
										if (this.iniCahce.ContainsKey(secString))
										{
											goto Block_16;
										}
										dictionary = new Dictionary<string, string>();
										this.iniCahce.Add(secString, dictionary);
									}
									else
									{
										if (!flag)
										{
											int num = text.IndexOf(";;");
											if (num >= 0)
											{
												text = text.Substring(0, num).Trim();
											}
											if (dictionary == null)
											{
												dictionary = new Dictionary<string, string>();
												this.iniCahce.Add("", dictionary);
											}
											string[] array = this.SplitKeyVal(text);
											if (array != null && array.Length >= 2)
											{
												array[0] = array[0].Trim();
												array[1] = array[1].Trim();
												if (!dictionary.ContainsKey(array[0]))
												{
													dictionary.Add(array[0], array[1]);
												}
											}
										}
									}
								}
							}
						}
					}
					goto IL_2F6;
					Block_16:
					MessageBox.Show(this.FileName + " \r\n“" + secString + "” 段重复, 请修改配置文件！");
					IL_2F6:
					streamReader.Close();
					result = true;
				}
			}
			return result;
		}
		private void MessageBoxShow(string p)
		{
			throw new NotImplementedException();
		}
		public void Save()
		{
			try
			{
				this.BackFile();
				if (!File.Exists(this.FileName))
				{
					FileStream fileStream = File.Create(this.FileName);
					fileStream.Close();
				}
				FileInfo fileInfo = new FileInfo(this.FileName);
				bool isReadOnly = fileInfo.IsReadOnly;
				if (!Directory.Exists(fileInfo.Directory.FullName))
				{
					Directory.CreateDirectory(fileInfo.Directory.FullName);
				}
				if (isReadOnly && fileInfo.Exists)
				{
					fileInfo.IsReadOnly = false;
				}
				StreamWriter streamWriter = new StreamWriter(File.Open(this.FileName, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite), Encoding.Default);
				foreach (KeyValuePair<string, Dictionary<string, string>> current in this.iniCahce)
				{
					string key = current.Key;
					streamWriter.WriteLine("[" + key + "]");
					Dictionary<string, string> value = current.Value;
					foreach (KeyValuePair<string, string> current2 in value)
					{
						streamWriter.WriteLine(current2.Key + "=" + current2.Value);
					}
					streamWriter.WriteLine("");
				}
				streamWriter.Close();
				if (isReadOnly)
				{
					fileInfo.IsReadOnly = true;
				}
				string path = this.FileName + ".bak";
				try
				{
					if (File.Exists(path))
					{
						File.Delete(path);
					}
				}
				catch
				{
				}
			}
			catch
			{
			}
		}
		private void BackFile()
		{
			string text = this.FileName + ".bak";
			try
			{
				if (File.Exists(text))
				{
					File.Delete(text);
				}
			}
			catch
			{
				return;
			}
			try
			{
				if (File.Exists(this.FileName))
				{
					File.Copy(this.FileName, text, true);
				}
			}
			catch
			{
			}
		}
		public string GetString(string section, string key)
		{
			return this.GetString(section, key, "");
		}
		public bool ContainsKey(string section, string key)
		{
			bool result;
			if (this.iniCahce.ContainsKey(section))
			{
				Dictionary<string, string> dictionary = this.iniCahce[section];
				if (dictionary.ContainsKey(key))
				{
					result = true;
					return result;
				}
			}
			result = false;
			return result;
		}
		public string GetString(string section, string key, string defValue)
		{
			string result;
			if (this.iniCahce.ContainsKey(section))
			{
				Dictionary<string, string> dictionary = this.iniCahce[section];
				if (dictionary.ContainsKey(key))
				{
					result = dictionary[key];
					return result;
				}
			}
			result = defValue;
			return result;
		}
		public bool WriteString(string section, string key, string str)
		{
			bool result = false;
			if (!string.IsNullOrEmpty(str))
			{
				str = str.Trim();
			}
			if (!this.iniCahce.ContainsKey(section))
			{
				this.iniCahce.Add(section, new Dictionary<string, string>());
			}
			if (!string.IsNullOrEmpty(key))
			{
				Dictionary<string, string> dictionary = this.iniCahce[section];
				if (!dictionary.ContainsKey(key) || dictionary[key] != str)
				{
					result = true;
				}
				dictionary[key] = str;
			}
			return result;
		}
		public int GetInt(string section, string key, int defValue)
		{
			int result;
			if (this.iniCahce.ContainsKey(section))
			{
				Dictionary<string, string> dictionary = this.iniCahce[section];
				if (dictionary.ContainsKey(key))
				{
					string text = dictionary[key];
					if (string.IsNullOrEmpty(text))
					{
						result = defValue;
						return result;
					}
                    result = UtilityFunc.ToInt(text, 0);
					return result;
				}
			}
			result = defValue;
			return result;
		}
		public bool GetBool(string section, string key, bool defValue)
		{
			bool result;
			if (this.iniCahce.ContainsKey(section))
			{
				Dictionary<string, string> dictionary = this.iniCahce[section];
				if (dictionary.ContainsKey(key))
				{
					string text = dictionary[key].ToUpper();
					if (string.IsNullOrEmpty(text))
					{
						result = defValue;
						return result;
					}
					result = (text == "是" || text == "YES" || text == "1" || text == "TRUE");
					return result;
				}
			}
			result = defValue;
			return result;
		}
		public bool WriteInt(string section, string key, int val)
		{
			return this.WriteString(section, key, val.ToString());
		}
		private string[] SplitKeyVal(string str)
		{
			int num = -1;
			for (int i = 0; i < str.Length; i++)
			{
				if (str[i] == '=')
				{
					num = i;
					break;
				}
			}
			string[] result;
			if (num > 0 && num < str.Length)
			{
				result = new string[]
				{
					str.Substring(0, num),
					str.Substring(num + 1)
				};
			}
			else
			{
				result = null;
			}
			return result;
		}
		private string ReadLine(StreamReader rd)
		{
			string text = "";
			string text2 = rd.ReadLine();
			string result;
			if (text2 == null)
			{
				result = null;
			}
			else
			{
				text2 = text2.Trim();
				if (!(text2 == ""))
				{
					text += text2;
				}
				result = text;
			}
			return result;
		}
		private static string GetSecString(string str)
		{
			int length = str.Length;
			string result;
			if (str[0] == '[')
			{
				int num = str.IndexOf(']');
				if (num > 0)
				{
					string text = str.Substring(1, num - 1);
					result = text.Trim();
					return result;
				}
			}
			result = "";
			return result;
		}
		public ICollection<string> GetAllSectionName()
		{
			return this.iniCahce.Keys;
		}
		public bool ContainSectionName(string secName)
		{
			return this.GetAllSectionName().Contains(secName);
		}
		public ICollection<string> GetSectionItemName(string sectName)
		{
			ICollection<string> result;
			if (this.iniCahce.ContainsKey(sectName))
			{
				Dictionary<string, string> dictionary = this.iniCahce[sectName];
				result = dictionary.Keys;
			}
			else
			{
				result = new List<string>();
			}
			return result;
		}
		public bool RemoveSectionItem(string sectName)
		{
			List<string> list = new List<string>(this.iniCahce.Keys);
			foreach (string current in list)
			{
				if (current == sectName || current.Contains(sectName + "\\"))
				{
					this.iniCahce.Remove(current);
				}
			}
			return true;
		}
	}
}
